﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;

namespace Zombie_Demo.World
{
    public enum TileType { Dirt_1, Dirt_2, Dirt_3, Dirt_4, Dirt_5, Water_1, Water_2 }; //relate each tile number to the string determinding its texture

    public class MapBlock
    {
        #region Variables
        private TileType[,] tiles; //will hold all the info about what tile goes where
        private Point worldCordinate;
        private NeighborBlockInfo nbi;
        #endregion

        #region Getters and Setters
        public TileType[,] TileData
        {
            get { return this.tiles; }
        }

        public Point WorldCordinate
        {
            get { return this.worldCordinate; }
        }

        public NeighborBlockInfo NBI
        {
            get { return this.nbi; }
            set { this.nbi = value; }
        }
        #endregion

        /// <summary>
        /// Creates a new block with terrain data in it
        /// </summary>
        /// <param name="worldCordinate">The place in the world that the block will start</param>
        public MapBlock(Point worldCordinate)
        {
            tiles = new TileType[64, 64]; //make a new tile array
            this.worldCordinate = worldCordinate;
            this.nbi = new NeighborBlockInfo();
        }

        /// <summary>
        /// Fill the tile with random dirt.
        /// </summary>
        /// <param name="r">The RNG generator</param>
        public void RandomDirt(Random r)
        {
            for(int x = 0; x < tiles.GetLength(0); x++)
                for (int y = 0; y < tiles.GetLength(1); y++)
                {
                    tiles[x, y] = (TileType)r.Next(5);
                }
            RandomWater(r);
        }

        /// <summary>
        /// Create the water in the first tile.
        /// </summary>
        /// <param name="r">The RNG generator</param>
        public void RandomWater(Random r)
        {
            int numWaters = r.Next(4);
            for (int i = 0; i <= numWaters; i++)
            {
                Vector2 randomVector = new Vector2(r.Next(tiles.GetLength(0) - 1), r.Next(tiles.GetLength(1) - 1));

                tiles[(int)randomVector.X, (int)randomVector.Y] = TileType.Water_1;
                Vector2 lastVector = randomVector;
                Vector2 newVector;
                int pChance = 100;

                bool inside = false;

                while (pChance > 0)
                {
                    newVector = lastVector + randomSquareDirection(r);

                    int counter = 0;
                    while (!inside)
                    {
                        if (lastVector.X <= 0 || lastVector.X > tiles.GetLength(0) || lastVector.Y <= 0 || lastVector.Y > tiles.GetLength(1))
                        {
                            newVector = lastVector + randomSquareDirection(r);
                            if (counter > 20)
                                 break;
                            counter++;
                        }
                        else
                        {
                            inside = true;
                        }
                    }

                    try
                    {
                        tiles[(int)lastVector.X, (int)lastVector.Y] = (TileType)r.Next(5, 7); //TODO: last vector doesnt know bound ranges.
                    }
                    catch (Exception e)
                    {
                        Utilities.ErrorManager.AddError(e);
                    }

                    pChance -= r.Next(6);
                }
            }
        }

        /// <summary>
        /// Returns a random normalized direction on the 8 compass points. Values will be -1, 0, or 1.
        /// </summary>
        /// <param name="r">The RNG generator</param>
        /// <returns>The normalized Vector2 of a random cardinal point.</returns>
        private static Vector2 randomSingleDirection(Random r)
        {
            int randomX = r.Next(-1, 2);
            int randomY = r.Next(-1, 2);

            return new Vector2(randomX, randomY);
        }

        /// <summary>
        /// Returns a random normalized direction on the 4 compass points. Values will be -1, 0, or 1.
        /// </summary>
        /// <param name="r">The RNG generator</param>
        /// <returns>The normalized Vector2 of a random cardinal point.</returns>
        private static Vector2 randomSquareDirection(Random r)
        {
            int randomX = 0;
            int randomY = 0;

            if (r.Next(101) >= 50)
                randomX = r.Next(-1, 2);
            else
                randomY = r.Next(-1, 2);

            return new Vector2(randomX, randomY);
        }

        //public void SetNBI()
        //{
            
        //    if (World.PageSystem.Page.ContainsKey(new Point(this.worldCordinate.X - 6400, this.worldCordinate.Y)))
        //        nbi.Left = true;

        //    if (World.PageSystem.Page.ContainsKey(new Point(this.worldCordinate.X + 6400, this.worldCordinate.Y)))
        //        nbi.Right = true;

        //    if (World.PageSystem.Page.ContainsKey(new Point(this.worldCordinate.X, this.worldCordinate.Y + 6400)))
        //        nbi.Bottom = true;

        //    if (World.PageSystem.Page.ContainsKey(new Point(this.worldCordinate.X, this.worldCordinate.Y - 6400)))
        //        nbi.Top = true;

        //    //TODO: Still need to check case for diagonal blocks.

        //}
    }
}
